﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.DataModel;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class CreatPipeTags : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
                Autodesk.Revit.DB.View activeView = activeUIDocument.ActiveView;
				Selection selection = activeUIDocument.Selection;
				FrmPipeMark frmPipeMark = new FrmPipeMark();
				DialogResult dialogResult = DialogResult.OK;
				HYTagOperate hytagOperate = new HYTagOperate(commandData);
				do
				{
					if (dialogResult == DialogResult.OK)
					{
						dialogResult = frmPipeMark.ShowDialog();
					}
					if (dialogResult == DialogResult.Yes)
					{
						transaction = new Transaction(document, "水管标注");
						transaction.Start();
						string tagName = this.GetTagName(frmPipeMark.markContent);
						string markStyle = frmPipeMark.markStyle;
						if (!(markStyle == "single"))
						{
							if (!(markStyle == "auto"))
							{
								if (markStyle == "multiply")
								{
									hytagOperate.MarkMultiplyPipe(tagName, 550.0, frmPipeMark.isHaveSlash, frmPipeMark.markUpMiddleDown, frmPipeMark.isMarkMultiDisance, frmPipeMark.markMultiDistance, frmPipeMark.IsDelete);
								}
							}
							else
							{
								hytagOperate.MarkAutoPipe(tagName, 200.0, frmPipeMark.markDistance, frmPipeMark.markUpMiddleDown);
							}
						}
						else
						{
							hytagOperate.MarkSinglePipe(tagName, 200.0, frmPipeMark.markUpMiddleDown, frmPipeMark.IsDelete);
						}
						dialogResult = DialogResult.Yes;
						document.Regenerate();
						transaction.Commit();
					}
				}
				while (dialogResult != DialogResult.Cancel);
				frmPipeMark.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		private string GetTagName(string styleName)
		{
			string result = "";
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(styleName);
			if (num <= 4032281248u)
			{
				if (num <= 2065846456u)
				{
					if (num != 1998588885u)
					{
						if (num == 2065846456u)
						{
							if (styleName == "style51")
							{
								result = "水管外径标注";
							}
						}
					}
					else if (styleName == "style41")
					{
						result = "水管类型外径标高标注";
					}
				}
				else if (num != 2200214503u)
				{
					if (num != 3944395331u)
					{
						if (num == 4032281248u)
						{
							if (styleName == "style6")
							{
								result = "水管管径标高标注";
							}
						}
					}
					else if (styleName == "style61")
					{
						result = "水管外径标高标注";
					}
				}
				else if (styleName == "style21")
				{
					result = "水管类型外径标注";
				}
			}
			else if (num <= 4082614105u)
			{
				if (num != 4049058867u)
				{
					if (num != 4065836486u)
					{
						if (num == 4082614105u)
						{
							if (styleName == "style5")
							{
								result = "水管管径标注";
							}
						}
					}
					else if (styleName == "style4")
					{
						result = "水管类型管径标高标注";
					}
				}
				else if (styleName == "style7")
				{
					result = "水管标高标注";
				}
			}
			else if (num != 4099391724u)
			{
				if (num != 4116169343u)
				{
					if (num == 4149724581u)
					{
						if (styleName == "style1")
						{
							result = "水管系统缩写标注";
						}
					}
				}
				else if (styleName == "style3")
				{
					result = "水管类型标高标注";
				}
			}
			else if (styleName == "style2")
			{
				result = "水管类型管径标注";
			}
			return result;
		}
	}
}
